package com.jmp.sms;

import com.google.common.collect.Maps;
import one.stand.util.AssertUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class ComplexSmsClient implements SmsClient {
    private String DEFALUT_APPID;
    private Map<String, SmsClient> smsClientMap = Maps.newHashMap();

    public void addClient(SmsClient smsClient) {
        AssertUtil.nonNull(smsClient.getAppId(), "client code 不能为空");
        if (StringUtils.isBlank(DEFALUT_APPID)) {
            this.DEFALUT_APPID = smsClient.getAppId();
        }
        smsClientMap.put(smsClient.getAppId(), smsClient);
    }

    @Override
    public String getAppId() {
        return this.DEFALUT_APPID;
    }

    @Override
    public void sendSmsCode(SmsClientCodeRequest request) {
        if (StringUtils.isBlank(request.getAppId())) {
            request.setAppId(getAppId());
        }
        SmsClient smsClient = smsClientMap.get(request.getAppId());
        smsClient.sendSmsCode(request);
    }

    @Override
    public void sendBatchSms(List<String> phones, Integer type, String params) {
        SmsClient smsClient = smsClientMap.get(getAppId());
        smsClient.sendBatchSms(phones, type, params);
    }
}
